package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class UpdateSubject {

	public static void main(String[] args) {

		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return;
		}
		String url = "jdbc:mysql://localhost:3306/java46?useUnicode=true&characterEncoding=utf8";
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入要修改的课程号：");
		int id = sc.nextInt();
		sc.nextLine();
		System.out.println("请输入名称:");
		String sname = sc.nextLine();
		System.out.println("请输入课时：");
		int hour = sc.nextInt();
		System.out.println("请输入年级：");
		int gradeId = sc.nextInt();
		Connection con = null;
		Statement st = null;
		try {
			con = DriverManager.getConnection(url, "suncy", "suncy");
			System.out.println("Connection实现类：" + con.getClass().getName());
			st = con.createStatement();
			// String sql =
			// "insert into subject(subjectname,classhour,gradeid)values('数据库编程',12,3)";
			String sql = "update subject set subjectname='" + sname
					+ "',classhour=" + hour + ",gradeid=" + gradeId
					+ " where id=" + id;
			System.out.println(sql);
			int r = st.executeUpdate(sql);
			System.out.println("修改了" + r + "条");

		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			try {
				if (st != null)
					st.close();
			} catch (SQLException e1) {
				// TODO 自动生成的 catch 块
				e1.printStackTrace();
			}

			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}

		}

	}

}
